<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>编辑服务菜单</title>
    <link rel="stylesheet" th:href="@{/src/css/layui.css}">
</head>
<body>

<form class="layui-form" style="width: 80%;" id="menuF">

    <input type="hidden" name="parentId" th:value="${serverMenu.parentId}"/>
    <input type="hidden" name="id" th:value="${serverMenu.id}"/>
    <div class="layui-form-item">
        <label class="layui-form-label">服务菜单</label>
        <div class="layui-input-block">
            <input type="text" id="serverName" class="layui-input"
                   lay-verify="required" placeholder="请输入服务菜单名" name="serverName" th:value="${serverMenu.serverName}">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">服务图片</label>
        <div class="layui-input-block">
            <button type="button" class="layui-btn" id="onePicUpload"><i class="layui-icon"></i>上传文件</button><br />
            <img th:src="@{${serverMenu.serverImg}}" class="layui-upload-img" id="preview" style="width: 150px;margin-top: 5px">
            <p id="uploadError"></p>
            <input type="hidden" name="serverImg" id="newsImg" th:value="${serverMenu.serverImg}"/>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">服务描述</label>
        <div class="layui-input-block">
            <textarea placeholder="请输入服务描述" class="layui-textarea linksDesc"
                      lay-verify="required" name="description" >[[${serverMenu.description}]]</textarea>
        </div>
    </div>
    <div class="layui-form-item">
        <div class="layui-input-block">
            <button class="layui-btn" lay-submit="" lay-filter="menuForm">立即提交</button>
        </div>
    </div>
</form>
<script th:src="@{/src/layui.js}"></script>
<script>
    layui.config({
        base:'/src/app/'
    }).use(['form','layer','jquery','upload'],function () {
        var form = layui.form,
            $ = layui.jquery,
            layer = layui.layer,
            upload = layui.upload;

        //服务图片上传
        upload.render({
            elem:'#onePicUpload', //绑定元素
            url:'/server/upload',//上传图片的请求接口 ;上传成功服务器端返回文件的路径
            before:function (obj) { //图片上传之前的回调
                //预读本地文件，如果是多文件，则会遍历。
                obj.preview(function (index,file,result) {//result 得到文件base64编码，比如图片
                    //显示图片
                    $("#preview").attr("src",result);

                })
            },
            done:function (result) {
                if(result.code == 200){
                    $("#newsImg").val(result.data.src);
                    layer.msg('文件上传成功',{icon:1},function () {
                        //将服务器端返回来的图片路径，存储在隐藏域中
                    })
                }else{
                    layer.msg('上传失败',{icon:5})
                }
            }
        })
        form.on('submit(menuForm)',function (data) {
            alert(JSON.stringify(data.field));
            $.ajax({
                url:'/server/updateMenu',
                type:'post',
                dataType:'json',
                data:data.field,
                success:function (result) {
                    if(result.code == 200){
                        layer.msg('编辑成功',{icon:1,time:1000},function () {
                            parent.layer.closeAll("iframe");
                            parent.location.reload();
                        })
                    }else{
                        layer.msg(result.message,{icon:5});
                    }
                }
            })

            return false;
        })




    })
</script>
</body>
</html>